package principle.ocp.improve;

/**
 * 开闭原则：对修改（使用方）关闭，对扩展（提供方）打开
 * 把shape类变成抽象的，之后有了新图形也只需要基础shape并重写draw方法，不用修改使用方
 */
public class oc {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
        graphicEditor.drawShape(new otherGraphic());
    }
}

//绘图，使用方
class GraphicEditor {
    //接收对象、调用方法就可以了
    public void drawShape(Shape s) {
        s.draw();
    }    }

//shape类，基类
abstract class Shape {
    int m_type;
    public abstract void draw();
}

class Rectangle extends Shape {
    Rectangle() {        super.m_type = 1;    }
    @Override
    public void draw() {
        System.out.println("绘制矩形");
    }}

class Circle extends Shape {
    Circle() {        super.m_type = 2;    }
    public void draw() {
        System.out.println("绘制圆形");
    }}

class Triangle extends Shape {
    Triangle() {
    super.m_type = 3;
}
    public void draw() {
        System.out.println("绘制三角形");
    }}
class otherGraphic extends Shape{
    otherGraphic(){super.m_type = 4; }
    @Override
    public void draw() {
        System.out.println("绘制新图形");
    }
}